/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */

package tests;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import modelLayer.ChartecCode;

import org.junit.Test;

import dbLayer.DbChartecCode;

public class TestDbChartecCode {
	DbChartecCode dbCC = new DbChartecCode();

	/**
	 * Test if the retrieving of a charteccode works
	 */
	@Test
	public void testRetrieveByCode() {
		ChartecCode cc = dbCC.retrieveByCode(8261);
		if(cc != null) {
			System.out.println("ChartecCode info:" +
					"\ncode: " + cc.getChartecCode() +
					"\nshippingPlace: " + cc.getShippingPlace() +
					"\nhasDeliveryNo: " + cc.hasDeliveryNo() +
					"\nSucces!\n"
					);
		} else {
			fail("Retrival of chartecCode failed\n");
		}
	}
	
	@Test
	public void testRetrieveAll() {
		ArrayList<ChartecCode> list = dbCC.retrieveAll();
		if(list.size() > 0) {
			System.out.println("Succes!\n");
		} else {
			fail("Retrival of all ChartecCodes failed\n");
		}
	}
	
	/**
	 * Insert charteccode
	 * Update charteccode
	 * Delete charteccode
	 */
	@Test
	public void testInsertUpdateDelete() {
		//Insert 
		ChartecCode cc = new ChartecCode(1111, "insertTest", true);
		try {
			dbCC.insert(cc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(dbCC.retrieveByCode(1111) != null) {
			System.out.println("ChartecCode data succesfully inserted in database\n");
		} else { 
			fail("Insertion failed\n");
		}

		//Update
		ChartecCode ccChange = dbCC.retrieveByCode(1111);
		ccChange.setShippingPlace("updateTest");
		try {
			dbCC.update(ccChange);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ChartecCode updatedCC = dbCC.retrieveByCode(1111);
		if(updatedCC.getShippingPlace().equals("updateTest")) {
			System.out.println("Updating was successfull\n"
					+ "shippingPlace: " + updatedCC.getShippingPlace() + "\n");
		} else {
			fail("Updating failed\n");
		}

		//Delete
		try {
			dbCC.delete(updatedCC);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(dbCC.retrieveByCode(1111) == null) {
			System.out.println("Deletion was successfully\n");
		} else {
			fail("Deletion failed\n");
		}	
	}
}
